/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pua.uniandes.SimuladorConcurrencia;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import pua.uniandes.servicios.ManejoUsuariosBeanRemote;

/**
 *
 * @author jd.velasquez3209
 */
public class SimuladorConcurrencia {

    private ManejoUsuariosBeanRemote testEJB;
    private static final int CANTIDAD_PAQUETE = 22;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new SimuladorConcurrencia();
    }

    public SimuladorConcurrencia() {


        File f = new File("Archivo Reporte Tiempos.csv");

        try {

            final PrintWriter pw = new PrintWriter(f);

            Properties props = new Properties();
            props.load(new FileInputStream("jndi.properties"));
            InitialContext ctx = new InitialContext(props);
            testEJB = (ManejoUsuariosBeanRemote) ctx.lookup("java:global/Experimento1TransmisionVideo/Experimento1TransmisionVideo-ejb/ManejoUsuariosBean!pua.uniandes.servicios.ManejoUsuariosBeanRemote");


            for (int i = 0; i < 300; i++) {

                Thread t = new Thread() {

                    public void run() {
                        simularCarga(pw);
                    }
                };
                t.start();

            }




        } catch (NamingException nex) {
            nex.printStackTrace();
        } catch (FileNotFoundException fnfex) {
            fnfex.printStackTrace();
        } catch (IOException ioex) {
            ioex.printStackTrace();
        }

    }

    public void simularCarga(PrintWriter pw) {


        double tInicio = System.currentTimeMillis();

        String response = testEJB.request("127.0.0.1");
        System.out.println(response);

        String ipAddress = response.split(":")[0];

        int puerto = Integer.parseInt(response.split(":")[1]);

        recibirVideo(ipAddress, puerto, pw, tInicio);

    }

    public void recibirVideo(String ipAddress, int puerto, PrintWriter pw, double tInicio) {
        try {


            DatagramSocket socket = new DatagramSocket(
                    puerto, InetAddress.getByName("localhost"));

            for (int i = 0; i < CANTIDAD_PAQUETE; i++) {



                DatagramPacket dp = new DatagramPacket(new byte[1024], 1024);

                File f = new File("video" + i + ".txt");


                recibirPaquete(socket, dp, f, i);
                double tFinal = System.currentTimeMillis();

                double tRespuesta = tFinal - tInicio;
                pw.println(tRespuesta + ";");
                tInicio = System.currentTimeMillis();

            }


        } catch (IOException ex) {
//            Logger.getLogger(SimuladorConcurrencia.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    public void recibirPaquete(DatagramSocket ds, DatagramPacket dp, File f, int num) {
        try {


//            FileOutputStream inFile = new FileOutputStream(f);

            ds.receive(dp);
            String finPaquete = new String(dp.getData());
            finPaquete = finPaquete.trim();
            while (!finPaquete.equals("Ya termino")) {
//                inFile.write(dp.getData(), 0, dp.getLength());
                ds.receive(dp);
                finPaquete = new String(dp.getData());
                finPaquete = finPaquete.trim();
                if (finPaquete.equals("Ya termino")) {
                    System.out.println("datos:" + finPaquete + " paquete NO. " + num);
                }
            }
//            inFile.close();
        } catch (IOException ex) {
            Logger.getLogger(SimuladorConcurrencia.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
